package it.fuzzylogics.fuzzywidgetsLibrary.client.FuzzyTabPanel;

import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Widget;
import it.fuzzylogics.fuzzywidgetsLibrary.client.FuzzyListeners.FClickListener;
import it.fuzzylogics.fuzzywidgetsLibrary.client.FuzzyListeners.FClickListenerHandler;

import java.util.Vector;


public class FTabPanel extends Composite {
    HorizontalPanel hPanel;
    AbsolutePanel bottomPanel;
    AbsolutePanel container;
    AbsolutePanel hPanelContaier;
    AbsolutePanel bottomSeparetorContainer;
    AbsolutePanel tabBarRestPanel;


    TabPanelListenerCollection listeners=new  TabPanelListenerCollection();

    int width, height;

    int barHeight = 20;
    int itemWidth = 80;

    int linesHeight = 1;

    Vector<Widget> items = new Vector();
    Vector<Widget> contents = new Vector();
    Vector<AbsolutePanel> separators = new Vector();

    int openItemIndex = -1;
    int bottomWSub = 0;
    int bottomHSub = 0;


    public FTabPanel() {

        initGui();
    }

    public FTabPanel(int bottomWSub, int bottomHSub) {
        this.bottomWSub = bottomWSub;
        this.bottomHSub = bottomHSub;

        initGui();
    }

    private void initGui() {
        container = new AbsolutePanel();
        bottomPanel = new AbsolutePanel();
        hPanelContaier = new AbsolutePanel();
        bottomSeparetorContainer = new AbsolutePanel();
        tabBarRestPanel = new AbsolutePanel();

        tabBarRestPanel.setStyleName("FTabPanel-TabBar-rest");

        hPanel = new HorizontalPanel();

        hPanel.setSpacing(0);

        hPanelContaier.setStyleName("FTabPanel-TabBar");
        bottomPanel.setStyleName("FTabPanel-Bottom");

        hPanel.setHorizontalAlignment(HorizontalPanel.ALIGN_LEFT);
        hPanel.setVerticalAlignment(HorizontalPanel.ALIGN_BOTTOM);


        container.add(bottomPanel);
        container.add(hPanelContaier, 0, 0);
        hPanelContaier.add(hPanel, 0, 0);
        hPanelContaier.add(bottomSeparetorContainer);

        bottomSeparetorContainer.add(tabBarRestPanel);


        initWidget(container);

    }

    public void setPixelSize(int w, int h) {
        super.setPixelSize(w, h);

        width = w;
        height = h;

        hPanelContaier.setPixelSize(w, barHeight + linesHeight);

        hPanelContaier.setWidgetPosition(bottomSeparetorContainer, 0, barHeight);
        bottomSeparetorContainer.setPixelSize(w, linesHeight);

        container.setWidgetPosition(bottomPanel, 0, barHeight);
        bottomPanel.setPixelSize(w - bottomWSub, h - barHeight - bottomHSub);


        if (openItemIndex >= 0)
            contents.get(openItemIndex).setPixelSize(w - bottomWSub, h - barHeight - bottomHSub);

        adjustHPanelSize();
    }

    public void insertTab(final Widget item, final Widget content, int index) {
        items.insertElementAt(item, index);
        contents.insertElementAt(content, index);

        AbsolutePanel separator = new AbsolutePanel();
        separator.setStyleName("FTabPanel-TabBar-separator");
        separators.insertElementAt(separator, index);
        //item.setPixelSize(itemWidth,barHeight);

        item.setStyleName("FTabPanel-TabBar-item");


        content.setVisible(false);


        FClickListener.setClickEvent(item.getElement(), new FClickListenerHandler() {

            public void onClick() {
                openTab(items.indexOf(item));
            }
        });

        hPanel.insert(item, index);
        bottomPanel.add(content, 0, 0);

        adjustHPanelSize();
    }

    public void addTab(final Widget item, final Widget content) {
        items.add(item);
        contents.add(content);

        AbsolutePanel separator = new AbsolutePanel();
        separator.setStyleName("FTabPanel-TabBar-separator");
        separators.add(separator);
        //item.setPixelSize(itemWidth,barHeight);

        item.setStyleName("FTabPanel-TabBar-item");


        content.setVisible(false);


        FClickListener.setClickEvent(item.getElement(), new FClickListenerHandler() {

            public void onClick() {
                openTab(items.indexOf(item));
            }
        });

        hPanel.add(item);
        bottomPanel.add(content, 0, 0);

        adjustHPanelSize();
    }

    private void adjustHPanelSize() {
        int tot = 0;

        for (int i = 0; i < hPanel.getWidgetCount(); i++)
            hPanel.remove(0);


        for (int i = 0; i < items.size(); i++)
            hPanel.add(items.get(i));

        for (int i = 0; i < hPanel.getWidgetCount(); i++) {
            int barItemWidth = hPanel.getWidget(i).getOffsetWidth();

            bottomSeparetorContainer.add(separators.get(i), tot, 0);
            separators.get(i).setPixelSize(barItemWidth, linesHeight);

            tot += barItemWidth;
        }

        hPanel.setPixelSize(tot, barHeight);
        bottomSeparetorContainer.setWidgetPosition(tabBarRestPanel, tot, 0);
        tabBarRestPanel.setPixelSize(width - tot - bottomWSub, linesHeight);

    }

    public void removeTab(int index) {

        contents.get(index).removeFromParent();
        items.get(index).removeFromParent();

        items.remove(index);
        contents.remove(index);


        separators.get(index).removeFromParent();
        separators.remove(index);

        if (index == openItemIndex)
            openItemIndex = -1;

        adjustHPanelSize();
    }

    public void removeTab(Widget content) {
        for (int i = 0; i < contents.size(); i++)
            if (content == contents.get(i)) {
                removeTab(i);
                return;
            }
    }


    public void openTab(int index) {
        if (openItemIndex >= 0) {
            contents.get(openItemIndex).setVisible(false);
            separators.get(openItemIndex).setStyleName("FTabPanel-TabBar-separator");
            items.get(openItemIndex).setStyleName("FTabPanel-TabBar-item");
        }

        contents.get(index).setVisible(true);
        contents.get(index).setPixelSize(width - bottomWSub, height - barHeight - bottomHSub);


        separators.get(index).setStyleName("FTabPanel-TabBar-separator-selected");
        items.get(index).setStyleName("FTabPanel-TabBar-item-selected");
        openItemIndex = index;


        adjustHPanelSize();

        listeners.fireOnTabOpened(index);

    }

    public Widget getItem(int index) {
        return items.get(index);

    }

    public Widget getContent(int index) {
        return contents.get(index);

    }

    public int getItemCount() {
        return items.size();
    }
   public void addTabPanelListener(TabPanelListener listener)
   {
       listeners.add(listener);
   }
   public int getOpenTabIndex()
   {
       return openItemIndex;
   }

}
